/**
 * <p>Title: Speed.java</p>
 * <p>Description: CS 343 Final Project. This class will set the speed of the game</p>
 * @author Tim Olmstead, Brady Fetting, Erik Brown, Thales Kirk
 * @email timothy.olmstead@my.uwrf.edu, brady.fetting@my.uwrf.edu, 
 * 			erik.brown@my.uwrf.edu, thales.kirk@my.uwrf.edu
 * @date November 15, 2013
 */

package Components;

import GameState.Game;

public class Speed {

	private int speed;
	private int topSpeed;
	private long lastSpeedCheck;
	private final long SPEEDCHECKGAP = 400;

	public Speed() {
		// speed of background & objects * 4 mph
		topSpeed = 0;
		speed = Game.getGameSpeed() * 4;
		lastSpeedCheck = System.currentTimeMillis();
	}

	/**
	 * @return speed
	 * @description gradually speed up the game based on target
	 */
	public int getSpeed() {
		if (System.currentTimeMillis() > lastSpeedCheck + SPEEDCHECKGAP) {
			double speedTarget = (Game.getGameSpeed() * 4)
					+ (Game.player.getDeltaX() * 3);
			if (speed > speedTarget) {
				lastSpeedCheck = System.currentTimeMillis();
				return --speed;
			} else if (speed < speedTarget) {
				lastSpeedCheck = System.currentTimeMillis();
				return ++speed;
			}
		}
		if (speed > topSpeed) {
			topSpeed = speed;
		}
		return speed;
	}

	/**
	 * @return topSpeed
	 * @description gets top speed
	 */
	public int getTopSpeed() {
		return topSpeed;
	}

	/**
	 * @return lastSpeedCheck
	 * @description gets the value of the lastSpeedCheck
	 */
	public synchronized long getLastSpeedCheck() {
		return lastSpeedCheck;
	}

	/**
	 * @param lastSpeedCheck
	 * @description sets the last speed that was checked
	 */
	public synchronized void setLastSpeedCheck(long lastSpeedCheck) {
		this.lastSpeedCheck = lastSpeedCheck;
	}

	/**
	 * @param speed
	 * @description sets the speed of the game
	 */
	public void setSpeed(int speed) {
		this.speed = speed;
	}
}
